package com.example.lizhanqi.livevideo.presenter;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.example.lizhanqi.livevideo.bean.HotBean;
import com.example.lizhanqi.livevideo.model.net.Networking;
import com.example.lizhanqi.livevideo.view.IHotUI;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Lizhanqi on 2016/9/17 0017.
 */
public class HotPresenter implements IOnResultPresnter {
    IHotUI view;
    Networking lastedNet;
    private static final String TAG = "李站旗";
    int Page = 0;
    String Type = "";
    String userid = "";
    boolean isFresh = false;
    private final LinkedList<HotBean.ContentBean.ListBean> listBeanLinkedList;
    int totalpage=0;
    private int curpage;
    //实例化的时候传入接口
    public HotPresenter(IHotUI view) {
        this.view = view;//传入后用来操作V层
        lastedNet = new Networking(this);
        listBeanLinkedList = new LinkedList<>();
    }

    /**
     * 请求网络,以及刷新数据
     * @param url
     * @param type
     * @param requestQueue
     * @param what
     * @param isFresh
     */
    public void requestGET(String url, String type, RequestQueue requestQueue, int what, boolean isFresh) {
        this.isFresh = isFresh;
        String path = url + "page=" + Page + "&type=" + type + "&userid=" + userid;
        lastedNet.requestGET(path, requestQueue, what);
    }

    /**
     * 加载更多
     * @param url
     * @param type
     * @param requestQueue
     * @param what
     */
    public void loadMore(String url,String type, RequestQueue requestQueue,int what){
        if(totalpage>curpage){
            String path = url + "page=" + ++curpage + "&type=" + type + "&userid=" + userid;
            lastedNet.requestGET(path, requestQueue, what);
            isFresh=false;
        }
    }
    /**
     * 网络请求成功
     * @param response
     */
    @Override
    public void ok(Response<String> response,int what) {
        if (response.isSucceed()) {
            parseJSON(response.get(),false);
        }
    }
    /**
     * 解析字符串
     * @param stringJOSN
     * @param ismore
     */
    public void parseJSON(String stringJOSN,boolean ismore) {
        Log.i(TAG, "parseJSON: " + stringJOSN);
        if (stringJOSN != null) {
            HotBean hotBean = JSONObject.parseObject(stringJOSN, HotBean.class);
            totalpage=hotBean.getContent().getPage().getTotalpage();
            curpage = hotBean.getContent().getPage().getCurpage();
            int state = hotBean.getState();
            if (state == 0) {
                List<HotBean.ContentBean.ListBean> list = hotBean.getContent().getList();
                if (list.size()>0){
                    for (HotBean.ContentBean.ListBean datas : list) {
                        if (isFresh) {
                            if (!isHave(datas.getRid())) {
                                listBeanLinkedList.addFirst(datas);
                            }
                        } else {
                            if (!isHave(datas.getRid())) {
                                listBeanLinkedList.addLast(datas);
                            }
                        }
                    }
                    //如果网络请求成功
                    view.succeed(hotBean.getContent().getBanner(),listBeanLinkedList);
                }else {//如果没有数据
                    view.noData();
                }
                isFresh=false;
            }
        }
    }

    /**
     * 判断是否有该数据
     * @param Rid
     * @return
     */
    public boolean isHave(int Rid) {
        if (listBeanLinkedList.size() != 0) {
            for (HotBean.ContentBean.ListBean lib : listBeanLinkedList) {
                if (lib.getRid() == Rid) {
                    return  true;
                }
            }
        }
        return false;
    }

    /**
     * 网络请求错误
     * @param i
     * @param s
     * @param o
     * @param e
     * @param i1
     * @param l
     */
    @Override
    public void error(int i, String s, Object o, Exception e, int i1, long l) {
        //如果请求网络失败
        view.failed();
    }


}

